package com.zhipu.balance.ui.home

import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.MotionEvent
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.SPUtils
import com.sjl.scanner.UsbKeyboardAutoScan
import com.zhipu.balance.R
import com.zhipu.balance.databinding.ActivityHomeBinding
import com.zhipu.balance.listener.AddOnClickListener
import com.zhipu.balance.mobile.temporaryClass.UniqueInfoById
import com.zhipu.balance.ui.BaseActivity
import com.zhipu.balance.ui.home.calculate.corn.CornActivity
import com.zhipu.balance.ui.home.calculate.dami.DaMiActivity
import com.zhipu.balance.ui.home.calculate.rapeseed.RapeseedActivity
import com.zhipu.balance.ui.home.calculate.rice.RiceActivity
import com.zhipu.balance.ui.home.calculate.soybean.SoybeanActivity
import com.zhipu.balance.ui.home.calculate.wheat.WheatActivity
import com.zhipu.balance.ui.vm.home.HomeViewModel
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.OpenSerialPort
import com.zhipu.balance.utils.request.InterfaceRequestUtils

/**
 * 首页
 */
class HomeActivity : BaseActivity<ActivityHomeBinding>() {
    //VM
    private var homeViewModel: HomeViewModel? = null


    /**
     * usb键盘模式扫码示例，无需EditText接收,推荐
     */
    private val usbKeyboardAutoScan: UsbKeyboardAutoScan = UsbKeyboardAutoScan()





    override fun initCreate() {
        super.initCreate()
        InitializeUtils.AppComponent()?.doInjectionMain(this)
        homeViewModel = ViewModelProvider(this, viewModelFactory)[HomeViewModel::class.java]
    }



    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_home
    }

    override fun onStart() {
        super.onStart()
        /**
         * 切换到2000
         */
        OpenSerialPort.SwitchTo2000()
    }


    override fun initView() {
        super.initView()
        binding.viewModel = homeViewModel
    }





    override fun initViewObservable() {
        super.initViewObservable()
        /**
         * 扫描枪回调
         */
        usbKeyboardAutoScan.setOnScanListener {
            InterfaceRequestUtils.getUniqueInfoById(it,object : AddOnClickListener {
                override fun onSkipClicked(sKip: String?) {
                    if(!TextUtils.isEmpty(sKip)){
                        var mUniqueInfoById = GsonUtils.fromJson(sKip, UniqueInfoById::class.java)
                        if(null != mUniqueInfoById){
                            when(mUniqueInfoById.productCategoryText){
                                "稻谷" -> {
                                    var mBundle = Bundle()
                                    mBundle.putString("ProductCategoryText",mUniqueInfoById.productCategoryText)
                                    ActivityUtils.startActivity(RiceActivity::class.java,mBundle)
                                }
                                "小麦" -> {
                                    //小麦
                                    var mBundle = Bundle()
                                    mBundle.putString("ProductCategoryText",mUniqueInfoById.productCategoryText)
                                    ActivityUtils.startActivity(WheatActivity::class.java,mBundle)
                                }
                                "玉米" -> {
                                    //玉米
                                    var mBundle = Bundle()
                                    mBundle.putString("ProductCategoryText",mUniqueInfoById.productCategoryText)
                                    ActivityUtils.startActivity(CornActivity::class.java,mBundle)
                                }
                                "大米" -> {
                                    //大米
                                    var mBundle = Bundle()
                                    mBundle.putString("ProductCategoryText",mUniqueInfoById.productCategoryText)
                                    ActivityUtils.startActivity(DaMiActivity::class.java,mBundle)
                                }
                                "大豆" -> {
                                    //大豆
                                    var mBundle = Bundle()
                                    mBundle.putString("ProductCategoryText",mUniqueInfoById.productCategoryText)
                                    ActivityUtils.startActivity(SoybeanActivity::class.java,mBundle)
                                }
                                "油菜籽" -> {
                                    //油菜籽
                                    var mBundle = Bundle()
                                    mBundle.putString("ProductCategoryText",mUniqueInfoById.productCategoryText)
                                    ActivityUtils.startActivity(RapeseedActivity::class.java,mBundle)
                                }

                            }
                        }
                    }
                }
            })
        }

    }


    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        val defaultEd = SPUtils.getInstance().getString("SerialNumber")
        if (defaultEd == "扫码枪") {
            if (usbKeyboardAutoScan.isIntercept && event!!.keyCode != KeyEvent.KEYCODE_BACK) { //不处理返回键
                usbKeyboardAutoScan.analysisKeyEvent(event)
                return true //防止输入框接收事件
            }
        }
        return super.dispatchKeyEvent(event)
    }
}